/*
 * @(#)ConfigurationBuilderTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.config;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.Document;

import com.nhncorp.lucy.common.util.ResourceLoader;
import com.nhncorp.lucy.common.util.XMLDOMUtil;
import com.nhncorp.usf.core.config.AttributeInfo.ATTRIBUTE_NAME;
import junit.framework.Assert;

/**
 * {@link ConfigurationBuilder} TestCase.
 *
 * @author Web Platform Development Team
 */
public class ConfigurationBuilderTestCase {
    static Document document;
    static Log log = LogFactory.getLog(ConfigurationBuilderTestCase.class);

    @BeforeClass
    public static void initialize() throws Exception {
        ResourceLoader loader = new ResourceLoader();
        Document document = XMLDOMUtil.loading(loader
                .getResourceAsStream("configuration.xml"), true);
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.build(document.getDocumentElement());
    }

    @AfterClass
    public static void destory() throws Exception {
        document = null;
    }

    @Test
    public void getAttribute() throws Exception {
        AttributeInfo attributeInfo = AttributeInfo.getInstance();
        //Assert.assertEquals(attributeInfo.getAttribute(ATTRIBUTE_NAME.schemaVersion), "2.1");
        Assert.assertEquals(attributeInfo.getAttribute(ATTRIBUTE_NAME.runtimeConfigBuilder),
                            "com.nhncorp.usf.core.parser.ConfigDOMBuilder");
        Assert.assertEquals(attributeInfo.getAttribute(ATTRIBUTE_NAME.exceptionHandler),
                            "com.nhncorp.usf.core.exception.DefaultExceptionHandler");
    }

    @Test
    public void getResultTypes() throws Exception {
        ResultTypeInfo resultTypeInfo = ResultTypeInfo.getInstance();
        Assert.assertEquals(resultTypeInfo.getResultTypeInfos().size(), 7);
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("forward"), "com.nhncorp.usf.core.result.ForwardResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("redirect"), "com.nhncorp.usf.core.result.RedirectResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("template.freemarker"), "com.nhncorp.usf.core.result.template.FreemarkerResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("direct.html"), "com.nhncorp.usf.core.result.direct.HtmlResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("direct.json"), "com.nhncorp.usf.core.result.direct.JSonResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("mock"), "com.nhncorp.usf.core.test.MockResult");
        Assert.assertEquals(resultTypeInfo.getResutlTypeInfo("none"), "com.nhncorp.usf.core.result.NoneResult");
    }

    @Test
    public void getInterceptors() throws Exception {
        InterceptorInfo interceptorInfo = InterceptorInfo.getInstance();

        // default-interceptor-ref
        Assert.assertEquals("testStack", interceptorInfo.getDefaultInterceptorRef());

        // interceptor
        Assert.assertEquals("com.nhncorp.usf.core.interceptor.test.TestInterceptor1",
                            interceptorInfo.getInterceptorClass("test1"));
        Assert.assertEquals("com.nhncorp.usf.core.interceptor.test.TestInterceptor2",
                            interceptorInfo.getInterceptorClass("test2"));
        Assert.assertEquals("com.nhncorp.usf.core.interceptor.test.TestInterceptor3",
                            interceptorInfo.getInterceptorClass("test3"));

        // interceptor-stack
        InterceptorStackInfo interceptorStackInfo =
                interceptorInfo.getInterceptorStackInfo("testStack");
        Assert.assertNotNull(interceptorStackInfo);
        List<String> interceptorRefs = interceptorStackInfo.getInterceptorRefs();
        Assert.assertEquals(3, interceptorRefs.size());
        Assert.assertEquals("test1", interceptorRefs.get(0));
        Assert.assertEquals("test2", interceptorRefs.get(1));
        Assert.assertEquals("test3", interceptorRefs.get(2));
    }

    @Test
    public void connectorInfoTest() {
        ConnectorInfos connectorInfos = ConnectorInfos.getInstance();
        ConnectorInfo connectorInfoLocal = connectorInfos.getConnectorInfo("localSpring");
        Assert.assertNull(connectorInfoLocal.getInvoker());
        Assert.assertEquals("lucy://local", connectorInfoLocal.getAddress());
    }

}
